/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.entity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = Constants.TABLE_NAME_PREFIX + "keystore")
@NamedQueries({
		@NamedQuery(name = KeyStoreEntity.QUERY_ALL, query = "SELECT ks FROM KeyStoreEntity AS ks"),
		@NamedQuery(name = KeyStoreEntity.QUERY_NAME, query = "SELECT ks FROM KeyStoreEntity AS ks "
				+ "WHERE ks.name = :name") })
public class KeyStoreEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final String QUERY_ALL = "kse.all";

	public static final String QUERY_NAME = "kse.name";

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@Column(unique = true)
	private String name;

	@Enumerated(EnumType.STRING)
	private KeyStoreType keyStoreType;

	private String path;

	private String password;

	private int slotListIndex;

	@Enumerated(EnumType.STRING)
	private KeyStoreState keyStoreState;

	public KeyStoreEntity() {
		super();
	}

	public KeyStoreEntity(String name, KeyStoreType keyStoreType, String path,
			String password) {
		this(name, keyStoreType, path, password, 0);
	}

	public KeyStoreEntity(String name, KeyStoreType keyStoreType, String path,
			String password, int slotListIndex) {
		this.name = name;
		this.keyStoreType = keyStoreType;
		this.path = path;
		this.password = password;
		this.slotListIndex = slotListIndex;
	}

	public long getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public KeyStoreType getKeyStoreType() {
		return this.keyStoreType;
	}

	public void setKeyStoreType(KeyStoreType keyStoreType) {
		this.keyStoreType = keyStoreType;
	}

	public String getPath() {
		return this.path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getSlotListIndex() {
		return this.slotListIndex;
	}

	public void setSlotListIndex(int slotListIndex) {
		this.slotListIndex = slotListIndex;
	}

	public KeyStoreState getKeyStoreState() {
		return this.keyStoreState;
	}

	public void setKeyStoreState(KeyStoreState keyStoreState) {
		this.keyStoreState = keyStoreState;
	}

	@Override
	public String toString() {
		return "KeyStoreEntity [id=" + this.id + ", name=" + this.name
				+ ", keyStoreType=" + this.keyStoreType + ", path=" + this.path
				+ "]";
	}

	public static List<KeyStoreEntity> getList(EntityManager entityManager) {
		Query query = entityManager.createNamedQuery(QUERY_ALL);
		return query.getResultList();
	}

	public static boolean hasKeyStore(EntityManager entityManager, String name) {
		Query query = entityManager.createNamedQuery(QUERY_NAME);
		query.setParameter("name", name);
		List<KeyStoreEntity> resultList = query.getResultList();
		return !resultList.isEmpty();
	}

	@Transient
	public void save(KeyStoreEntity changedKeyStore) {
		this.name = changedKeyStore.getName();
		this.keyStoreType = changedKeyStore.getKeyStoreType();
		this.path = changedKeyStore.getPath();
		this.password = changedKeyStore.getPassword();
		this.slotListIndex = changedKeyStore.getSlotListIndex();
		this.keyStoreState = null;
	}
}
